package me.green.ui.dialog;

import java.awt.*;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import me.green.bean.User;
import me.green.dao.UserDao;
import me.green.ui.LocalCache;
import me.green.ui.EtnFrame;
import me.green.ui.global.UIResources;
import me.green.ui.global.UIStyle;
import me.green.ui.global.i18n.UIText;
import me.green.ui.system.TrayMenu;

import static me.green.ui.global.UIResources.ICON_UNLOCK;

/**
 * 解锁窗口
 *
 * @author green
 */
public class UnLockDialog extends JDialog {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JLabel labInfo = null;

    private JPasswordField txtPwd = null;
    private JButton btnUnLock = null;

    private static MenuItem itemLock = null;
    private static TrayMenu trayMenu = null;

    private UnLockDialog() {
        super();
        initialize();
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void showInputPasswordDialog(TrayMenu trayMenu) {
        UnLockDialog.trayMenu = trayMenu;
        itemLock = trayMenu.getItemLock();
        UnLockDialog.itemLock = itemLock;
        new UnLockDialog();
    }

    /**
     * This method initializes txtPwd
     *
     * @return javax.swing.JPasswordField
     */
    private JPasswordField getTxtPwd() {
        if (txtPwd == null) {
            txtPwd = new JPasswordField();
            txtPwd.setBounds(new Rectangle(162, 40, UIStyle.INPUT_WIDTH, UIStyle.INPUT_HEIGHT));
            txtPwd.setFont(UIStyle.defaultFont());
            txtPwd.setMargin(new Insets(5, 5, 5, 5));
            txtPwd.addKeyListener(new java.awt.event.KeyAdapter() {
                @Override
                public void keyPressed(java.awt.event.KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        unlock();
                    }
                }
            });
        }
        return txtPwd;
    }

    /**
     * This method initializes btnUnLock
     *
     * @return javax.swing.JButton
     */
    private JButton getBtnUnLock() {
        if (btnUnLock == null) {
            btnUnLock = new JButton();
            btnUnLock.setBounds(new Rectangle(162, 94, 80, UIStyle.INPUT_HEIGHT));
            btnUnLock.setText(UIStyle.text("unlock"));
            btnUnLock.setFont(UIStyle.defaultFont());
            btnUnLock.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    unlock();
                }
            });
        }
        return btnUnLock;
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(540, 200);
        this.setTitle(UIStyle.text(UIText.TITLE));
        this.setIconImage(new ImageIcon(this.getClass().getResource(UIResources.ICON_TRAY)).getImage());
        this.setContentPane(getJContentPane());
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            labInfo = new JLabel();
            labInfo.setBounds(new Rectangle(18, 6, 120, 120));
            labInfo.setText("");
            labInfo.setHorizontalAlignment(SwingConstants.CENTER);
            labInfo.setIcon(new ImageIcon(getClass().getResource(ICON_UNLOCK)));
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.setBackground(SystemColor.control);
            jContentPane.add(getTxtPwd(), null);
            jContentPane.add(getBtnUnLock(), null);
            jContentPane.add(labInfo, null);
        }
        return jContentPane;
    }

    /**
     * 解锁
     */
    private void unlock() {
        String pwd = new String(txtPwd.getPassword()).trim().toLowerCase();
        User user = new UserDao().login(LocalCache.getUser().getUserName(), pwd);
        if (user != null) {
            itemLock.setLabel("Lock");
            trayMenu.removeIcon();
            EtnFrame frame = LocalCache.getFrameApp();
            if (frame != null) {
                frame.setVisible(true);
                frame.setState(JFrame.NORMAL);
            }
            this.dispose();
            LocalCache.setLock(false);
        } else {
            JOptionPane.showMessageDialog(UnLockDialog.this, UIStyle.text(UIText.ERROR_WRONG_PASSWORD));
            txtPwd.setText("");
            txtPwd.requestFocus();
        }
    }

}
